package com.lenovo.spider.process;

import com.lenovo.spider.domain.Task;

import java.util.function.Function;
import java.util.function.Predicate;

/**
 * @author <a href="mailto:liuhan3@lenovo.com">HanL(liuhan3)</a>
 * on 2018/3/21 17:59
 */
public interface TaskProcess {

    default Function<Task, Task> doIf(Predicate<Task> predicate, Function<Task, Task> fn) {
        Function<Task, Task> defaultFn = t -> t;
        Function<Task, Task> pFn = t -> {
            try {
                return fn.apply(t);
            } catch (Exception e) {
                return t.error(e);
            }
        };
        return t -> (predicate.test(t) ? pFn : defaultFn).apply(t);
    }

}
